import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import RouteList from '../components/RouteList.vue'

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/route-list', component: RouteList, meta: { requiresAuth: true }},
    {
        path: '/route/:routeId/notes',
        component: () => import('../components/NoteList.vue'),
        meta: { requiresAuth: true }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫，未登录不能访问管理页
router.beforeEach((to, from, next) => {
    const isLogin = !!localStorage.getItem('loginUser')
    if (to.meta.requiresAuth && !isLogin) {
        next('/login')
    } else {
        next()
    }
})


export default router